package org.hackystat.devcathlon.ui.page.scoreboard.logopanel;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.imageio.ImageIO;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.matches.MatchesSession;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;

/**
 * Display the team logos to the scoreboard.
 * 
 * @author Daniel Arakaki
 */
public class LogoPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Set up and display a list of team logos.
   * 
   * @param id The Wicket id from the html markup.
   * @param matchList A list match names.
   */
  public LogoPanel(String id, List<String> matchList) {
    super(id);
    
    String noMatch = "";
    if (matchList.isEmpty()) {
      noMatch = "No logos...";

      //Set<Team> teams = match.getTeams();
      List<Team> teamList = new ArrayList<Team>();
      
      ListView listView = new ListView("TeamLogos", teamList) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(ListItem logoItem) {
          final Team teamLogo = (Team) logoItem.getModelObject();
          Link teamLink = new Link("teamLink", logoItem.getModel()) {
            /** Support serialization. */
            private static final long serialVersionUID = 1L;
            
            @Override
            public void onClick() {
              setResponsePage(new TeamsProfile());
            }
          };
          try {
            BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
            BufferedImage image = ImageIO.read(new File(teamLogo.getTeamLogoPath()));
            resource.setImage(image);
            teamLink.add(new Image("TeamLogo", resource));
          }
          catch (IOException e) {
            //teamLink.add(new Image("TeamLogo"));
            e.printStackTrace();
          }
          logoItem.add(teamLink);
        }
      };
      add(listView); 
    }
    
    else {
      DevcathlonSession devcathlonSession = DevcathlonSession.get();
      MatchesSession matchesSession = devcathlonSession.getMatchesSession();
      Match match = matchesSession.getMatch(matchList.get(0));
    
    //Insert team logos
    Set<Team> teams = match.getTeams();
    List<Team> teamList = new ArrayList<Team>(teams);
    
    ListView listView = new ListView("TeamLogos", teamList) {

      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem logoItem) {
        final Team teamLogo = (Team) logoItem.getModelObject();
        Link teamLink = new Link("teamLink", logoItem.getModel()) {
          /** Support serialization. */
          private static final long serialVersionUID = 1L;
          
          @Override
          public void onClick() {
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(teamLogo.getName());
            setResponsePage(new TeamsProfile());
          }
        };
        try {
          BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
          BufferedImage image = ImageIO.read(new File(teamLogo.getTeamLogoPath()));
          resource.setImage(image);
          teamLink.add(new Image("TeamLogo", resource));
        }
        catch (IOException e) {
          teamLink.add(new Image("TeamLogo"));
        }
        logoItem.add(teamLink);
      }
    };
    add(listView); 
  }
  add(new Label("noMatches", noMatch));
  }
  

}
